package com.hy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.entity.Lesson;
import com.hy.mapper.LessonMapper;
import com.hy.service.LessonService;
import com.hy.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class LessonServiceImpl extends ServiceImpl<LessonMapper, Lesson> implements LessonService {
    @Autowired
    private TeacherService teacherService;
    @Override
    public List<Lesson> getAllLessons() {
        LambdaQueryWrapper<Lesson> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lesson::getDeleteFlag, 0)
               .orderByDesc(Lesson::getCreateTime);
        return list(wrapper);
    }

    @Override
    public Lesson getLessonById(Long id) {
        LambdaQueryWrapper<Lesson> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lesson::getId, id)
               .eq(Lesson::getDeleteFlag, 0);
        return getOne(wrapper);
    }

    @Override
    @Transactional
    public boolean addLesson(Lesson lesson) {
        lesson.setDeleteFlag(0);
        lesson.setCreateTime(new Date());
        lesson.setUpdateTime(new Date());
        return save(lesson);
    }

    @Override
    @Transactional
    public boolean updateLesson(Lesson lesson) {
        lesson.setUpdateTime(new Date());
        return updateById(lesson);
    }

    @Override
    @Transactional
    public boolean deleteLesson(Long id) {
        LambdaUpdateWrapper<Lesson> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Lesson::getId, id)
                    .set(Lesson::getDeleteFlag, 1)
                    .set(Lesson::getUpdateTime, new Date());
        return update(updateWrapper);
    }

    @Override
    public List<Lesson> getLessonsByTeacherId(Long teacherId) {
        LambdaQueryWrapper<Lesson> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Lesson::getTeacherId, teacherId)
               .eq(Lesson::getDeleteFlag, 0)
               .orderByDesc(Lesson::getCreateTime);
        return list(wrapper);
    }

    @Override
    public Page<Lesson> pageLessons(int pageNum, int pageSize, String lessonName, Integer teacherId) {
        Page<Lesson> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Lesson> queryWrapper = new QueryWrapper<>();

        // 动态添加查询条件
        if (StringUtils.hasText(lessonName)) {
            queryWrapper.like("lessonName", lessonName);
        }
        if (teacherId != null) {
            queryWrapper.eq("teacherId", teacherId);
        }

        // 按创建时间倒序排列
        queryWrapper.orderByDesc("createTime");

        Page<Lesson> page1 = page(page, queryWrapper);
        page1.getRecords().forEach(lesson -> {
            lesson.setTeacherName(teacherService.getLessonTeacherName(lesson.getTeacherId()).getTeacherName());
        });
        return page1;
    }
}
